//
//  LYOrderListCell.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/26.
//

import UIKit

class LYOrderListCell: UITableViewCell {
    
    static let leftRMargin = 16.0
    static let top = 20.0
    private lazy var backgroundImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = false
        iv.image = UIImage(named: "order_list_bg")
        return iv
    }()
    
    private lazy var iconImgView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = true
        iv.image = UIImage(named: "logo_icon")
        return iv
    }()
    
    private lazy var tagImgView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.image = UIImage(named: "order_list_tag_1")
        return iv
    }()
    
    private lazy var tagLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12, weight: .semibold)
        label.textAlignment = .center
        label.textColor = UIColor(hex: "#FFFFFF")
//        label.text = "Overdue"
        label.transform = CGAffineTransform(rotationAngle: -0.11)
        return label
    }()

    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 18)
        label.textAlignment = .center
        label.numberOfLines = 0
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .left
        label.text = "saappppss"
        return label
    }()
    
    private lazy var countLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
        label.textAlignment = .left
        label.textColor = UIColor(hex: "#000000")
        label.text = "9000"
        return label
    }()
    
    private lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textAlignment = .left
        label.textColor = UIColor(hex: "#000000")
        label.text = "Borrowing date：02/17/2025"
        label.numberOfLines = 0
        return label
    }()
    
    lazy var repaymentButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "repayment_button_bg"), for: .normal)
        button.setTitleColor(UIColor(hex: "#FFFFFF"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        button.setTitle(LYlocalizedString("Repayment"), for: .normal)
        button.isUserInteractionEnabled = false
        return button
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        
        commonInitSubviews()
    }
    
    func commonInitSubviews() {
        addSubview(backgroundImageView)
        backgroundImageView.addSubview(iconImgView)
        backgroundImageView.addSubview(titleLabel)
        backgroundImageView.addSubview(tagImgView)
        backgroundImageView.addSubview(tagLabel)
        backgroundImageView.addSubview(countLabel)
        backgroundImageView.addSubview(dateLabel)
        backgroundImageView.addSubview(repaymentButton)
        setLayoutSubviews()
    }
    
    func setLayoutSubviews() {
        backgroundImageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(LYOrderListCell.top)
            make.bottom.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(LYOrderListCell.leftRMargin)
        }
        
        iconImgView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.leading.equalToSuperview().offset(5)
            make.width.height.equalTo(16 * xScale)
        }
        
        tagImgView.snp.makeConstraints { make in
            make.centerY.equalTo(iconImgView).offset(-2)
            make.trailing.equalToSuperview().offset(-20)
        }
        
        tagLabel.snp.makeConstraints { make in
            make.leading.bottom.equalTo(tagImgView)
            make.trailing.equalTo(tagImgView).offset(-20)
            make.top.equalTo(tagImgView).offset(4)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(iconImgView)
            make.leading.equalTo(iconImgView.snp.trailing).offset(5)
            make.trailing.equalTo(tagImgView.snp.leading).offset(-5)
        }
        
        repaymentButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview().offset(17)
            make.trailing.equalToSuperview().offset(-10)
            make.height.equalTo(39 * xScale)
            make.width.equalTo(129 * xScale)
        }
        
        countLabel.snp.makeConstraints { make in
            make.top.equalTo(repaymentButton)
            make.leading.equalToSuperview().inset(15)
            make.trailing.equalTo(repaymentButton.snp.leading).offset(-5)
            make.height.equalTo(25)
        }
        
        dateLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(countLabel)
            make.bottom.equalTo(repaymentButton)
        }
    }
    
    func configure(with septenier: OrderListSeptenier) {
        
        self.titleLabel.text = septenier.argue
        self.countLabel.text = septenier.justibility
        self.dateLabel.text = (septenier.maciair?.mneitude ?? "") + ": " + (septenier.maciair?.lavlet ?? "")
        self.tagLabel.text = septenier.maciair?.veriid
        if let furtclearlyet = septenier.maciair?.furtclearlyet,
           !furtclearlyet.isEmpty {
            repaymentButton.setTitle(furtclearlyet, for: .normal)
            repaymentButton.isHidden = false
        } else {
            repaymentButton.isHidden = true
        }
        
        if let axiobarress = septenier.maciair?.axiobarress,
           let orderState = OrderState(rawValue: axiobarress) {
            self.tagImgView.image = UIImage(named: orderState.tagImageName)
        }
        
        if let baseURL = septenier.juxtaette {
            let queryString = NetworkManager.shared.commonParameters.toURLQueryString()
            let imageUrl = "\(baseURL)?\(queryString)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
            iconImgView.sd_setImage(with: URL(string: imageUrl ?? ""), placeholderImage: nil, options: .retryFailed)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

enum OrderState: String, RawRepresentable {
    case gardenment = "gardenment" // 放款成功
    case prepare = "prepare" // 逾期
    case surgaster = "surgaster" // 申请中
    case interview = "interview" // 审或者放款中
    case chair = "chair" // 订单完成

    var tagImageName: String {
        switch self {
        case .gardenment:
            return "order_list_tag_1"
        case .prepare:
            return "order_list_tag_2"
        case .surgaster:
            return "order_list_tag_3"
        case .interview:
            return "order_list_tag_4"
        case .chair:
            return "order_list_tag_5"
        }
    }
}
